package com.pengtu.gsj.service.impl.check.lzgl.enforSup;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONObject;
import com.pengtu.gsj.entity.app.Attachment;
import com.pengtu.gsj.entity.check.CheckTable;
import com.pengtu.gsj.service.check.CheckTableService;
import com.pengtu.gsj.service.check.lzgl.enforSup.SupMechPerfectService;
import com.pengtu.gsj.service.system.SystemManageService;
import com.pengtu.gsj.utils.MathsUtils;


@Service
@Transactional
public class SupMechPerfectServiceImpl implements SupMechPerfectService {
	@Autowired
	private CheckTableService checkTableService;
	
	@Autowired
	private SystemManageService systemManageService;
	
	
	@Override
	public Map<String, Object> getDevelopPlanmMap(String startTime, String endTime, CheckTable checkTable, String tag)throws Exception{
		Map<String, Object> map = new HashMap<String, Object>();
		
		JSONObject jsonObject = JSONObject.parseObject(checkTable.getRetrieval());
		//制定了执法督导制度得分
		double yearFull = Double.parseDouble((String)jsonObject.get("yearFull"));
		//督导检查常态化得分
		double yearPart = Double.parseDouble((String)jsonObject.get("yearPart"));
		//每缺少一次督导检查扣分
		double poitMark = Double.parseDouble((String)jsonObject.get("poitMark"));
		//督导检查中发现问题，能及时落实整改得分
		double reserveMark = Double.parseDouble((String)jsonObject.get("reserveMark"));
		//每有一次未落实整改的扣分
		//double firstStepScore = Double.parseDouble((String)jsonObject.get("firstStepScore"));
		//需要检查通报的总次数
		int thirdStepScore = Integer.valueOf((String)jsonObject.get("thirdStepScore"));
		//取到所有文件
		List<Attachment> list = systemManageService.getAllCheckAttachments(startTime, endTime, tag);
		//获取规章制度
		List<Attachment> gzzdList = systemManageService.getAllCheckAttachments("dczdjq_gzzd");
		//获取年度通知通报文件
		List<Attachment> tbwjList = systemManageService.getCheckAttachmentByTag(startTime, endTime, "dczdjq_tbwj");
		double total =0; 				//检查得分
		double percentage =0; 			//系统完善度
		double tableFull  = yearFull + yearPart + reserveMark;	//满分
		int years[] = new int[systemManageService.getCheckYear(endTime, startTime, 0)];
		for (int i = 0; i < years.length; i++) {
			years[i] = Integer.parseInt(startTime)+i;
		}
		
		if (gzzdList.size() > 0) {
			total += yearFull;
		} 
		
		if (tbwjList.size() >= thirdStepScore) {
			total += yearPart;
		} else if ((thirdStepScore - tbwjList.size())*poitMark >= yearPart){
			total += 0;
		} else {
			total += tbwjList.size()*poitMark;
		}
		
		total += reserveMark;
		
		map.put("sum",  MathsUtils.formatDouble(total,2));
		//判断完善度
		percentage = MathsUtils.divDouble(total *100 , tableFull, 2) ;
		checkTableService.updateCheckTable(checkTable, percentage, total, tableFull, true);
		map.put("percent", percentage);
		map.put("years", years);
		map.put("att", list);
		map.put("yearFull", yearFull);
		map.put("gzzdList", gzzdList);
		map.put("tbwjList", tbwjList);
		map.put("yearFull", yearFull);
		map.put("score", checkTable.getTestFull());
		map.put("checkTable", checkTable);
		
		return map;
	}
}
